package com.azi.baseconhecimento.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.azi.baseconhecimento.entity.Dica;
import com.azi.baseconhecimento.service.DicaService;

@Controller
public class DicaList {

	@Autowired
	private DicaService dicaService;

	@RequestMapping(value = "/dica/list.htm")
	public ModelAndView init(ModelMap map) {
		List<Dica> dicas = dicaService.buscarDicas(5);
		map.addAttribute("dicas", dicas);
		return new ModelAndView("/dica/dicaList.jsp");
	}

	@RequestMapping(value = "/dica/buscar.htm", method = RequestMethod.GET)
	public ModelAndView buscarDicas(ModelMap map, @RequestParam("texto")
	String texto, @RequestParam("tipo")
	String tipo) {
		List<Dica> dicas = null;

		if (tipo != null) {
			if (tipo.equalsIgnoreCase("titulo")) {
				dicas = dicaService.buscarDicasPorTitulo(texto);
			} else if (tipo.equalsIgnoreCase("titulo_descricao")) {
				dicas = dicaService.buscarDicasPorTituloDescricao(texto);
			} else if (tipo.equalsIgnoreCase("colaborador")) {
				dicas = dicaService.buscarDicasPorColaborador(texto);
			}
		}

		map.addAttribute("dicas", dicas);
		return new ModelAndView("/dica/dicaList.jsp");
	}


	@RequestMapping(value = "/dica/visualizarDicasCategoria.htm", method = RequestMethod.GET)
	public ModelAndView buscarDicas(ModelMap map, @RequestParam("categoria")
	String categoria) {
		List<Dica> dicas = dicaService.buscarDicasPorCategoria(categoria);

		map.addAttribute("dicas", dicas);
		return new ModelAndView("/dica/dicaList.jsp");
	}
}
